package com.sunny.myproject.storem.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.Criterion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunny.myproject.core.base.action.BaseAction;
import com.sunny.myproject.storem.entity.Store;
import com.sunny.myproject.storem.service.StoreService;

@Controller
@RequestMapping("/storem/store")
public class StoreAction extends BaseAction<Store>{

	private String title = "仓库";
	@Autowired
	private StoreService service;
	
	@Override
	@RequestMapping(params="list")
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		
		super.pager = super.pageStart(model, request,response, "storem/store","store",title);
		super.pager = super.pageEnd(model, service.num(criterions), pager);
		
		super.returnValue(model, service.find(criterions, super.pager.getFirstResults(), super.pager.getMaxResults()));
		return "storem/storeList";
	}

	@Override
	@RequestMapping(params="add")
	public String add(HttpServletRequest request, HttpServletResponse response) {
		super.add(title, request);
		return "storem/storeEdit";
	}

	@Override
	@RequestMapping(params="edit")
	public String edit(Model model, HttpServletRequest request,HttpServletResponse response) {
		super.returnValue(model, service.findById(super.edit(title, model, request)));
		return "storem/storeEdit";
	}

	@Override
	@RequestMapping(params="delete")
	public String delete(HttpServletRequest request,HttpServletResponse response) {
		service.deleteByIds(super.param2(request));
		return "redirect:/storem/store?list";
	}

	@Override
	@RequestMapping(params="save")
	public @ResponseBody String save(HttpServletRequest request,HttpServletResponse response,@ModelAttribute Store t) {
		service.save(t);
		return "storem/store?list";
	}

	@Override
	@RequestMapping(params="view")
	public String view(Model model, HttpServletRequest request,HttpServletResponse response) {
		super.returnViewValue(model, service.findById(super.view(title, model, request)));
		return "storem/storeEdit";
	}

	@RequestMapping(params="chart")
	public String chart(Model model, HttpServletRequest request,HttpServletResponse response){
		criterions = new ArrayList<Criterion>();
		List<Store> stores = service.list(criterions);
		String str = "";
		for (Store store : stores) {
			str +="['"+store.getName()+"',"+store.getAmount()+"],";
		}
		str = str.length()>0?str.substring(0, str.length()-1):"";
		model.addAttribute("data", str);
		return "storem/storeChart";
	}
}
